//
//  AddressChoiceViewController.swift
//  demo
//
//  Created by toro宇 on 2020/2/25.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit

class AddressChoiceViewController: UIViewController {

    // MARK: UI
    
    lazy var nav = HZCustomNavigationBar.customNavigationBar().then { (bar) in
        bar.barBackgroundColor = UIColor(hex: "fbfbfb")
        bar.shadowImageHidden = false
        /* 闭包 相当于block
         { (参数) -> (返回值) in
         
         }
         */
        bar.hz.setItemsToLeft([HZNavigationBarItem.create(normalImage: "back", barItemClickHandler: { (item) in
            self.navigationController?.popViewController(animated: true)
        }), HZNavigationBarItem.create(normalTitle: "广发银行ETCa卡申请", barItemClickHandler: {
            (item) in
        })])
    }
    
  lazy  var addressChoiceView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = UIColor.white
        
        let titleView = UIView()
        var tmpLab : UILabel?
        let titleAry = ["省份","城市","区县"]
        for i in 0...2{
            let titleLab = UILabel()
            titleLab.textAlignment = NSTextAlignment.center
            titleView.addSubview(titleLab)
            titleLab.font = UIFont.systemFont(ofSize: 13)
            titleLab.text = titleAry[i]
            titleLab.snp.makeConstraints({ (make) in
                if i == 0{
                    make.left.equalToSuperview()
                }else if i == 1{
                    make.left.equalTo(tmpLab!.snp.right)
                    make.width.equalTo(tmpLab!)
                }else{
                    make.left.equalTo(tmpLab!.snp.right)
                    make.width.equalTo(tmpLab!)
                    make.right.equalToSuperview()
                }
                make.bottom.equalToSuperview()
                make.top.equalToSuperview().offset(20)
            })
            tmpLab = titleLab
        }
        bgView.addSubview(titleView)
        titleView.snp.makeConstraints({ (make) in
            make.left.right.top.equalToSuperview()
        })
    
        let titleLine = UIView()
        titleLine.backgroundColor = UIColor.lightGray
        bgView.addSubview(titleLine)
        titleLine.snp.makeConstraints({ (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
            make.height.equalTo(0.5)
        })
    
    
    let addressView = UIView()
    var tempBtn : UIButton?
    
    let addressAry = [
        ["河南省"],
        ["郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市",],
        ["金水区","高兴区","郑东新区"]
    ]
    for i in 0...2{
        let addressBtn = UIButton()
        addressView.addSubview(addressBtn)
        addressBtn.setTitle("请选择", for: .normal)
        addressBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        addressBtn.setTitleColor(UIColor.gray, for: .normal)
        addressBtn.tag = 1000 + i
        addressBtn.actionBlock = { (sender) in
            // 地址选择
            let nowAry = addressAry[i]
            
            if let view = self.choiceAlertView{
                view.removeFromSuperview()
            }
            let alertView = AddressChoiceView.addressChoiceView(addressAry: nowAry, choiceBlock: { (addressStr) in
                self.choiceAlertView?.removeFromSuperview()
                sender.setTitle(addressStr, for: .normal)
                addressBtn.setTitleColor(UIColor.red, for: .normal)

                if sender.tag == 1000{
                    
                    (addressView.viewWithTag(1001) as! UIButton).setTitle("请选择", for: .normal)
                    (addressView.viewWithTag(1001) as! UIButton).setTitleColor(UIColor.gray, for: .normal)

                    (addressView.viewWithTag(1002) as! UIButton).setTitle("请选择", for: .normal)
                    (addressView.viewWithTag(1002) as! UIButton).setTitleColor(UIColor.gray, for: .normal)
                    

                }else if sender.tag == 1001{
                    (addressView.viewWithTag(1002) as! UIButton).setTitle("请选择", for: .normal)
                    (addressView.viewWithTag(1002) as! UIButton).setTitleColor(UIColor.gray, for: .normal)
                }
                
            })
            bgView.addSubview(alertView)
            self.choiceAlertView = alertView
            alertView.snp.makeConstraints({ (make) in
                make.top.equalTo(addressView.snp.bottom).offset(10)
//                make.bottom.equalToSuperview()
                make.centerX.equalTo(sender)
                make.width.equalTo(100)
            })
  
        }
        addressBtn.snp.makeConstraints({ (make) in
            if i == 0{
                make.left.equalToSuperview()
            }else if i == 1{
                make.left.equalTo(tempBtn!.snp.right)
                make.width.equalTo(tempBtn!)
            }else{
                make.left.equalTo(tempBtn!.snp.right)
                make.width.equalTo(tempBtn!)
                make.right.equalToSuperview()
            }
            make.top.bottom.equalToSuperview()
        })
        tempBtn = addressBtn
    }
    bgView.addSubview(addressView)
    addressView.snp.makeConstraints({ (make) in
        make.left.right.equalToSuperview()
        make.top.equalTo(titleView.snp.bottom)
        make.height.equalTo(40)
    })
    
    let addressLine = UIView()
    addressLine.backgroundColor = UIColor.lightGray
    bgView.addSubview(addressLine)
    addressLine.snp.makeConstraints({ (make) in
        make.left.right.equalToSuperview()
        make.top.equalTo(addressView.snp.bottom)
        make.height.equalTo(0.5)
    })
        return bgView
    }()
    
    weak var choiceAlertView:AddressChoiceView?
    
    let sureBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.red
        btn.setTitle("确定", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        btn.actionBlock = { _ in
            
            
        }
        return btn
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if let view = self.choiceAlertView {
            view.removeFromSuperview()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: "f4f4f4")

        view.addSubview(nav)
        view.addSubview(addressChoiceView)
        addressChoiceView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(nav.snp.bottom)
            make.height.equalTo(500)
        }
        view.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(45)
            make.top.equalTo(addressChoiceView.snp.bottom).offset(30)
        }
        
        // Do any additional setup after loading the view.
    }

}

class AddressChoiceView:UIView, UITableViewDataSource, UITableViewDelegate{
  
   var choiceBlock: ((String) -> Void)?
   private var addressAry = [String]()
   lazy  private var  addressTab:UITableView = {
        let tab = UITableView.init(frame: CGRect.zero, style: .plain)
        tab.separatorStyle = UITableViewCell.SeparatorStyle.none
        tab.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tab.delegate = self
        tab.dataSource = self
        tab.rowHeight = 40
        return tab
    }()
    
    public static func addressChoiceView(addressAry: [String],choiceBlock:  @escaping (String) -> Void) -> AddressChoiceView{
        let view = AddressChoiceView()
        view.addressAry = addressAry
        view.choiceBlock = choiceBlock
        view.initCustomView()
        return view
    }
    
    func initCustomView(){
        self.addSubview(addressTab)
        addressTab.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(addressAry.count * 40)
            make.bottom.equalToSuperview()
        }
    }
    
    
    //MARK: Deltegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return addressAry.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        if let lab = cell.viewWithTag(1000) as? UILabel{
            lab.text = addressAry[indexPath.row]
        }else{
            let lab = UILabel()
            cell.addSubview(lab)
            lab.tag = 1000
            lab.font = UIFont.systemFont(ofSize: 13)
            lab.textColor = UIColor.lightGray
            lab.text = addressAry[indexPath.row]
            lab.snp.makeConstraints { (make) in
                make.center.equalToSuperview()
            }
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let block = self.choiceBlock {
            block(addressAry[indexPath.row])
        }
    }
}
